package com.nononsenseapps.feeder.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.target.Target;
import timber.log.Timber;

import java.util.concurrent.ExecutionException;

/**
 * Makes sure proper caching is enabled for all image requests and that the cache can be used in offline scenarios also.
 */
public class GlideImageLoader {

    public static RequestManager glide(Context context) {
        return Glide.with(context);
    }

    public static void preDownLoad(Context context, String imgUrl) throws ExecutionException, InterruptedException {
        glide(context)
                  .load(imgUrl)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
       .submit().get()
       ;
    }

    public static RequestBuilder<Drawable> glide(Context context, String imgUrl, boolean allowDownload) {
        if (allowDownload) {
            return glide(context)
                    .load(imgUrl)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    ;
        } else {
            return glide(context)
                    .load(imgUrl)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }
    }

    public static RequestBuilder<GifDrawable> glideAsGif(Context context, String imgUrl, boolean allowDownload) {
        if (allowDownload) {
            return  glide(context)
                    .asGif()
                    .load(imgUrl)
                    //可以解决gif比较几种时 ，加载过慢  //DiskCacheStrategy.NONE
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        } else {
            return glide(context)
                    .asGif()
                    .load(imgUrl)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        }
    }

    public static void glideAsGifInto(Context context, String imgUrl,int width,int height,Target<Drawable> taget) {
        glide(context)
                      //.asGif()
                      .load(imgUrl)
                      .fitCenter()
                      .override(width,height)
                    //可以解决gif比较几种时 ，加载过慢  //DiskCacheStrategy.NONE
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .into(taget);
    }

    public static RequestBuilder<Bitmap> glideAsBitmap(Context context, String imgUrl, boolean allowDownload) {
        if (allowDownload) {
            return glide(context)
                    .asBitmap()
                    .load(imgUrl)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        } else {
            return glide(context)
                    .asBitmap()
                    .load(imgUrl)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
        }
    }

    public static void  displayImage(Context context, String path, ImageView imageView) {
        try {
            GlideImageLoader.glide(context,path,true)
                    .error(android.R.drawable.ic_menu_gallery)
                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .into(imageView);
        } catch ( IllegalArgumentException e) {
            // Could still happen if we have a race-condition?
            Timber.e(e);
        }
    }
}
